﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using DigitalRune.Windows.Docking;

namespace SharpLab
{
    public partial class Communication : DockableForm
    {
        private Comm _com = new Comm();
        /// <summary>
        /// Konstruktor
        /// </summary>
        public Communication()
        {
            InitializeComponent();
            _com.RaiseReadEvent += new EventHandler<ReadEventArgs>(_com_RaiseReadEvent);
        }
        /// <summary>
        /// Wird ausgelöst, wenn eine Zeichenkette vom Anschluss gelesen wurde
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void _com_RaiseReadEvent(object sender, ReadEventArgs e)
        {
            tslStatus.Text = e.incomData;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Communication_Load(object sender, EventArgs e)
        {
            List<string> _Devices = _com.DeviceList;

            for (int i = 0; i < _Devices.Count; i++)
            {
                cbx_Devices.Items.Add(_Devices[i]);
            }
        }
        /// <summary>
        /// Testet die Kommunikation mit dem Ct'lab mit den gewählten Einstellungen
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Ereignisargumente</param>
        private void btTest_Click(object sender, EventArgs e)
        {
            _com.SendMessage("*:IDN?");
        }
        /// <summary>
        /// Anschluss wurde geändert
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Ereignisargumente</param>
        private void cbx_Devices_SelectedIndexChanged(object sender, EventArgs e)
        {
            _com.DeviceName = cbx_Devices.SelectedItem.ToString();
        }
        /// <summary>
        /// Anschluss öffnen
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Ereignisargumente</param>
        private void btOpen_Click(object sender, EventArgs e)
        {
            _com.OpenPort();
        }
        /// <summary>
        /// Anschluss schließen
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Ereignisargumente</param>
        private void btClose_Click(object sender, EventArgs e)
        {
            _com.ClosePort();
        }
    }
}
